package pojo.wizards;

import org.eclipse.jface.wizard.Wizard;

import com.alibaba.druid.pool.DruidDataSource;

import pojo.model.DsModel;
import pojo.template.GeneratorEntrance;
import pojo.template.MysqlDialect;
import pojo.template.OracleDialect;
import pojo.utils.DbUtils;
import pojo.utils.FileUtils;
import pojo.utils.PathKit;

public class JavaBeanWizard extends Wizard {

	DatabaseWizardPage databaseWizardPage;
	TableWizardPage tableWizardPage;
	PathWizardPage pathWizardPage;

	@Override
	public void addPages() {
		databaseWizardPage = new DatabaseWizardPage("DatabaseWizardPage");
		tableWizardPage = new TableWizardPage("TableWizardPage");
		pathWizardPage = new PathWizardPage("PathWizardPage");
		addPage(databaseWizardPage);
		addPage(tableWizardPage);
		addPage(pathWizardPage);
	}

	public static void main(String[] args) {
		DsModel dsModel = new DsModel();
		dsModel.setDriver("oracle.jdbc.driver.OracleDriver");
		dsModel.setUser("ibot2");
		dsModel.setPassword("ibot2");
		dsModel.setUrl("jdbc:oracle:thin:@111.202.106.150:1521:orcl");
		dsModel.setDbName("orcl");
		java.util.List<String> list = DbUtils.getTables(dsModel);
		list.remove("auth_user");

		DruidDataSource ds = new DruidDataSource();
		ds.setUrl("jdbc:oracle:thin:@111.202.106.150:1521:orcl");
		ds.setUsername("ibot2");
		ds.setPassword("ibot2");
		ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		// ds.setUrl("jdbc:mysql://localhost/manager_test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull");
		// ds.setUsername("root");
		// ds.setPassword("root");
		// ds.setDriverClassName("com.mysql.jdbc.Driver");
		String modelPackageName = "com.demo.model";
		String modelOutputDir = PathKit.getWebRootPath() + "/src/com/demo/model";
		GeneratorEntrance gen = new GeneratorEntrance(ds, modelPackageName, modelOutputDir);
		gen.addExcludedTable(list.toArray(new String[list.size()]));
		gen.setDialect(new OracleDialect());
		gen.generate();
	}

	@Override
	public boolean performFinish() {

		String modelPackageName = pathWizardPage.getBaseModelPackageName_();// "com.demo.model";
		// model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
		String modelOutputDir = pathWizardPage.getBaseModelOutputDir_();// PathKit.getWebRootPath() +
																		// "/src/com/demo/model";
		GeneratorEntrance gen = new GeneratorEntrance(databaseWizardPage.getDataSource(), modelPackageName,
				modelOutputDir);
		java.util.List<String> tables = tableWizardPage.getexcludedTables();
		gen.addExcludedTable(tables.toArray(new String[tables.size()]));
		DsModel d = (DsModel) FileUtils.readFile(FileUtils.getDataSourceFileName());
		gen.setDialect(d.getDataBaseType().equals("ORACLE") ? new OracleDialect() : new MysqlDialect());
		gen.generate();
		return true;
	}

	@Override
	public boolean canFinish() {
		if (PathWizardPage.class.getSimpleName()
				.equals(super.getContainer().getCurrentPage().getName())) {
			if (!pathWizardPage.isPageComplete())
				return false;
			return true;
		} else {
			return false;
		}
	}

}
